home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2474 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.0 KB  |  110 lines

  1. Path: locutus.rchland.ibm.com!usenet
  2. From: pstaite@vnet.ibm.com
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: === Repost : Interrupt driven object ===
  5. Date: 17 Jan 1996 21:55:46 GMT
  6. Organization: IBM OS/2 Device Driver Development  Rochester, MN
  7. Message-ID: <4djr92$1at3@locutus.rchland.ibm.com>
  8. References: <4dj02s$60q@newsbf02.news.aol.com>
  9. Reply-To: pstaite@vnet.ibm.com
  10. NNTP-Posting-Host: warpone.rchland.ibm.com
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. In <4dj02s$60q@newsbf02.news.aol.com>, awhang8367@aol.com (AWhang8367) writes:
  14. >
  15. >>In article <4d6imm$okq@newsbf02.news.aol.com>, awhang8367@aol.com says...
  16. >>
  17. >>  I have been struggling to create an object that handles hardware
  18. >>interrupt directly.  Particularly with the setvect function as it takes
  19. >in
  20. >>pointers to interrupts 
  21. >>and not to objects.  Two things I can do (and I have accomplished ther
  22. >>former):
  23.  
  24. Please, please, please everyone, read the FAQ before posting or 
  25. re-posting a question.  <donning white robe>  "We will now have a public
  26. reading from the FAQ..."
  27.  
  28.  
  29. ==============================================================================
  30.  
  31. Q111: Is the type of "ptr-to-member-fn" different from "ptr-to-fn"?
  32.  
  33. Yep.
  34.  
  35. Consider the following function:
  36.  
  37.     int f(char a, float b);
  38.  
  39. If this is an ordinary function, its type is:    int (*)      (char,float);
  40. If this is a method of class Fred, its type is:  int (Fred::*)(char,float);
  41.  
  42. ==============================================================================
  43.  
  44. Q112: How do I pass a ptr to member fn to a signal handler, X event callback,
  45.    etc?
  46.  
  47. Don't.
  48.  
  49. Because a member function is meaningless without an object to invoke it on, you
  50. can't do this directly (if The X Windows System was rewritten in C++, it would
  51. probably pass references to OBJECTS around, not just pointers to fns; naturally
  52. the objects would embody the required function and probably a whole lot more).
  53.  
  54. As a patch for existing software, use a top-level (non-member) function as a
  55. wrapper which takes an object obtained through some other technique (held in a
  56. global, perhaps).  The top-level function would apply the desired member
  57. function against the global object.
  58.  
  59. E.g., suppose you want to call Fred::memfn() on interrupt:
  60.  
  61.     class Fred {
  62.     public:
  63.       void memfn();
  64.       static void staticmemfn();    //a static member fn can handle it
  65.       //...
  66.     };
  67.  
  68.     //wrapper fn remembers the object on which to invoke memfn in a global:
  69.     Fred* object_which_will_handle_signal;
  70.     void Fred_memfn_wrapper() { object_which_will_handle_signal->memfn(); }
  71.  
  72.     main()
  73.     {
  74.       /* signal(SIGINT, Fred::memfn); */   //Can NOT do this
  75.       signal(SIGINT, Fred_memfn_wrapper);  //Ok
  76.       signal(SIGINT, Fred::staticmemfn);   //Also Ok
  77.     }
  78.  
  79. Note: static member functions do not require an actual object to be invoked, so
  80. ptrs-to-static-member-fns are type compatible with regular ptrs-to-fns (see ARM
  81. ["Annotated Reference Manual"] p.25, 158).
  82.  
  83. ==============================================================================
  84.  
  85. Q113: Why do I keep getting compile errors (type mismatch) when I try to use a
  86.    member function as an interrupt service routine? 
  87.  
  88. This is a special case of the previous two questions, therefore read the
  89. previous two answers first.
  90.  
  91. Non-static member functions have a hidden parameter that corresponds to the
  92. 'this' pointer.  The 'this' pointer points to the instance data for the
  93. object.  The interrupt hardware/firmware in the system is not capable of
  94. providing the 'this' pointer argument.  You must use "normal" functions (non
  95. class members) or static member functions as interrupt service routines.
  96.  
  97. One possible solution is to use a static member as the interrupt service
  98. routine and have that function look somewhere to find the instance/member pair
  99. that should be called on interrupt.  Thus the effect is that a normal method
  100. is invoked on an interrupt, but for technical reasons you need to call an
  101. intermediate function first.
  102.  
  103. ==============================================================================
  104.  
  105.  
  106.  
  107. Phil Staite, team OS/2
  108. internet: pstaite@vnet.ibm.com  internal: pstaite@rchland
  109.  
  110.